import {requestConfig} from '@/common/config/requestConfig.js'//全局配置参数，包括加密是否开启的开关
import utils from '@/common/utils/util.js'
import CryptoJS from 'crypto-js'
import AES from 'crypto-js/aes'
import Base64 from 'crypto-js/enc-base64'
import MD5 from 'crypto-js/md5'
import SHA1 from 'crypto-js/sha1'
export default {
	encript(params) {
		// debugger;
		if(params.url) {
			var encryptFlag = false;
			if(handleUrlForPermission(params)){ 
				encryptFlag = true;
			}else {
				encryptFlag = false;
			}
			//小爱机器人不加密,人工客服不加密
			if(params.url.indexOf('robotService')>-1 || params.url.indexOf('datasearch/customerservice')>-1){
				encryptFlag = false;
			}
			//增加接口加密功能
			let sortData = JSON.parse(JSON.stringify({}));
			
			if(params.data && requestConfig.encryptOpen && encryptFlag){
				console.log('---加密前的入参----：',params.url,params.data)
				//对象按照key值进行排序
				Object.keys(params.data).sort().forEach(function(key) {
					sortData[key] = params.data[key];
				});
				//第一次base64加密
				let utf8Data = CryptoJS.enc.Utf8.parse(JSON.stringify(sortData))
				let base64Data = Base64.stringify(utf8Data);
				//md5加密
				let MD5Data = MD5(base64Data).toString();
				//sha1加密
				let SHA1Data = SHA1(MD5Data).toString();
				//截取sign与定义时间戳
				let signData = SHA1Data.substr(6,20);
				let timestamp = new Date().getTime();
				let addParam = {
					signext:signData,
					timestampext:timestamp
				};
				let newParamData =  Object.assign(addParam,sortData);
				//第二次base64加密
				let utf8DataAgain = CryptoJS.enc.Utf8.parse(JSON.stringify(newParamData))
				let twiceBase64Data = Base64.stringify(utf8DataAgain);	
				//AES加密
				let utf8AES = CryptoJS.enc.Base64.parse(requestConfig.AESKey);
				let AESKey = utf8AES.toString(CryptoJS.enc.Utf8);
				let encryptedbytes  = CryptoJS.AES.encrypt(twiceBase64Data,CryptoJS.enc.Utf8.parse(AESKey),{
					mode: CryptoJS.mode.ECB,
				});
				let requestData = encryptedbytes.toString();
				//最终请求发送的终极加密参数
				params.data = {
					data:requestData
				};
			}
			return params;
		}else {
			console.error('url不能为空~')
		}
		function handleUrlForPermission(params){
			let encript = false;
			//商城商品支付接口
			if(haveUrl('/mall-portal/mall/payment/createPaymentFor')){
				encript = true;
			}else {
				encript = false;
			}
			return encript;
			
			function haveUrl(str){
				// debugger
				return params.url.indexOf(str)>-1;
			}
			//商城app原12306业务支付接口
			// '/order/createWxPayApplet'
			// '/order/createWxPay'
			// '/order/createAlipay'
			// '/order/createAlipayApplet'
			// '/order/supplement/createWxPayApplet'
			// '/order/supplement/createWxPay'
			// '/order/supplement/createAlipayApplet'
			// '/order/supplement/createAlipay'
			// '/order/wapUnionPay'
			// '/order/supplement/wapUnionPay'
			//商城app商品下单支付接口
			// /mall-portal/mall/payment/createPaymentForWx 
			// /mall-portal/mall/payment/createPaymentForWxApplet 
			// /mall-portal/mall/payment/createPaymentForAliApplet 
			// /mall-portal/mall/payment/createPaymentForAl 
		}
	},
	decript(res) {
		// debugger
		if(requestConfig.encryptOpen && typeof res.data.data == 'string'){
			if(res.data.success == false || res.data.success == 'False'){
				console.log('接口数据返回异常',res);
				resolve(res);
			}else {
				let tmpRes = res;
				let uncodeData = res.data.data;
				//base64解密
				let base64OBJ = CryptoJS.enc.Base64.parse(uncodeData.toString(CryptoJS.enc.Utf8));
				let base64Str = base64OBJ.toString(CryptoJS.enc.Utf8);
				//AES解密
				let encryptedHexStr = CryptoJS.enc.Hex.parse(base64Str);
				let encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
				//后端代码默认的是ECB模式
				let utf8AES = CryptoJS.enc.Base64.parse(requestConfig.AESKey);
				let AESKey = utf8AES.toString(CryptoJS.enc.Utf8);
				let decryptedbytes  = CryptoJS.AES.decrypt(encryptedBase64Str,CryptoJS.enc.Utf8.parse(AESKey),{
					mode: CryptoJS.mode.ECB,
				});
				let decryptedStr = CryptoJS.enc.Utf8.stringify(decryptedbytes)
				if(decryptedStr){
					if(decryptedStr.indexOf('{') > -1 || decryptedStr.indexOf('[') > -1){
						tmpRes.data.data = JSON.parse(decryptedStr);
					}else {
						//针对data里面是非对象字符串的情况，不然"12345"会被转成12345，如果超过16位，数字精确度就有误差
						tmpRes.data.data = decryptedStr;
					}
				}else {
					tmpRes.data.data = '';
				}
				console.log('---解密接口数据----：',tmpRes)
				return tmpRes;
			}
		}else {
			return res;
		}
	}
};